# app/services/simulation.py

#
# async def run_simulation(config_id: int, strategy_name: str) -> Dict:
#     config = await ParameterConfig.get(config_id=config_id).prefetch_related("user", "battery")
#     result = {}
#     # 获取环境输入数据
#     env_data = await EnvInputData.filter(config_id=config_id).order_by("time_point")
#     if not env_data:
#         raise ValueError("缺少环境输入数据，无法执行仿真。")
#
#     # 初始化电池状态
#     battery_energy = config.initial_energy
#
#     total_cost = Decimal("0.0")
#     total_emission = Decimal("0.0")
#     total_grid_purchase = Decimal("0.0")
#     solar_total = Decimal("0.0")
#     wind_total = Decimal("0.0")
#
#     for record in env_data:
#         # 构造策略输入
#         input_data = EnergyManagementInput(
#             battery_energy=battery_energy,
#             load=record.load_power,
#             solar_output=record.solar_irradiance,
#             wind_output=record.wind_speed,
#             price_grid=record.price_grid,
#             price_wind=record.price_wind,
#             price_solar=record.price_solar,
#             battery_capacity=config.battery.capacity,
#             battery_charge_efficiency=config.battery.charge_efficiency,
#             battery_discharge_efficiency=config.battery.discharge_efficiency,
#             battery_max_charge_power=config.battery.max_charge_power,
#             battery_max_discharge_power=config.battery.max_discharge_power,
#         )
#
#         if strategy_name == "rule":
#             # 调用策略函数
#             result = energy_management_strategy(input_data, config_id, strategy_name)
#
#         # 更新电池状态
#
#         battery_energy = result["battery_energy"] # 注意：此时电池剩余电量应在 input_data 中更新
#
#         # 保存每步系统状态
#         await SystemState.create(
#             config_id=config_id,
#             time_point=record.time_point,
#             solar_output=record.solar_irradiance,
#             wind_output=record.wind_speed,
#             battery_energy=battery_energy,
#             load=record.load_power,
#             price_grid=record.price_grid,
#             price_wind=record.price_wind,
#             price_solar=record.price_solar,
#             cost=result["total_cost"],
#             carbon_emission=result["total_emission"],
#             action=strategy_name
#         )
#
#         # 累加结果
#         total_cost += result["total_cost"]
#         total_emission += result["total_emission"]
#         total_grid_purchase += result["total_grid_purchase"]
#         solar_total += result["solar_supply"]
#         wind_total += result["wind_supply"]
#
#     # 保存策略总结果
#     await StrategyResult.create(
#         config_id=config_id,
#         strategy_name=strategy_name,
#         total_cost=total_cost,
#         total_emission=total_emission,
#         battery_usage_rate=result["battery_usage_rate"],
#         total_grid_purchase=total_grid_purchase,
#         grid_purchase=total_grid_purchase,
#         solar_supply=solar_total,
#         wind_supply=wind_total,
#     )
#
#     return {
#         "status": "simulation complete",
#         "total_cost": float(total_cost),
#         "total_emission": float(total_emission),
#         "total_grid_purchase": float(total_grid_purchase),
#         "steps": len(env_data),
#     }


